package club.ximeng.sliceupload.common.entity;

import club.ximeng.sliceupload.common.enums.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author liu xm
 * @date 2022-03-07 13:11
 */
@Data
public class ResponseResult<T> implements Serializable {
    /**
     * 错误码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String message;
    /**
     * 返回数据
     */
    private T data;


    public ResponseResult(ResultCodeEnum resultCodeEnum, T data) {
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();
        this.data = data;
    }

    public ResponseResult(ResultCodeEnum resultCodeEnum) {
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();
    }

    public ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(ResultCodeEnum.SUCCESS, data);
    }

    /**
     * 成功返回结果
     */
    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(ResultCodeEnum.SUCCESS);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> error(Integer code, String message) {
        return new ResponseResult<>(code, message);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> error(ResultCodeEnum resultCodeEnum) {
        return new ResponseResult<>(resultCodeEnum);
    }
}
